<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>shell 练习-统计网卡流量 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>shell 练习-统计网卡流量</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个脚本,检测你的网络流量，并记录到一个日志里。需要按照如下格式，并且一分钟统计一次（只需要统计外网网卡，假设网卡名字为eth0)：</p>
<p>2017-08-04 01:11<br>eth0 input: 1000bps<br>eth0 output : 200000bps    </p>
<p>################<br>2017-08-04 01:12<br>eth0 input: 1000bps<br>eth0 output : 200000bps    </p>
<p>提示：使用sar -n DEV  1 59 这样可以统计一分钟的平均网卡流量，只需要最后面的平均值。另外，注意换算一下，1byt=8bit</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">while</span> :</div><div class="line"><span class="keyword">do</span></div><div class="line">    DATE=`date +<span class="string">"%Y-%m-%d %H:%M"</span>`</div><div class="line">    LOG_PATH=/tmp/traffic_check/`date +%Y%m`</div><div class="line">    LOG_FILE=<span class="variable">$LOG_PATH</span>/traffic_check_`date +%d`.<span class="built_in">log</span></div><div class="line"></div><div class="line">    [ -d <span class="variable">$LOG_PATH</span>] || mkdir -p <span class="variable">$LOG_PATH</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">" <span class="variable">$DATE</span>"</span> &gt;&gt; <span class="variable">$LOG_FILE</span></div><div class="line">    sar -n DEV 1 59|grep Average|grep eth0|awk <span class="string">'&#123;print "\n",$2,"\t","input:",$5*1000*8,"bps","\t","\n",$2,"\t","output:",$6*1000*8,"bps" &#125;'</span> &gt;&gt; <span class="variable">$LOG_FILE</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"#####################"</span> &gt;&gt; <span class="variable">$LOG_FILE</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Shell 练习/30. shell 练习-统计网卡流量/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
